package com.runvp.rest.user;

import com.runvp.common.annotation.Log;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.SortSupport;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.enums.EnableStatus;
import com.runvp.common.util.ToolUtils;
import com.runvp.common.util.auth.SecurityUtils;
import com.runvp.service.biz.domain.BizCustomerRecharge;
import com.runvp.service.biz.domain.BizCustomerTranLog;
import com.runvp.service.biz.domain.BizCustomerWallet;
import com.runvp.service.biz.domain.BizRechargePackageItem;
import com.runvp.service.biz.handler.WechatPayHandler;
import com.runvp.service.biz.service.IBizCustomerRechargeService;
import com.runvp.service.biz.service.IBizCustomerTranLogService;
import com.runvp.service.biz.service.IBizCustomerWalletService;
import com.runvp.service.biz.service.IBizRechargePackageItemService;
import com.runvp.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户钱包Controller
 *
 * @author runvp
 * @date 2022-11-05
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/app/customer/wallet")
public class BizCustomerWalletController extends BaseController {

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IBizCustomerWalletService bizCustomerWalletService;
    @Autowired
    private IBizRechargePackageItemService rechargePackageItemService;
    @Autowired
    private IBizCustomerTranLogService bizCustomerTranLogService;
    @Autowired
    private WechatPayHandler wechatPayHandler;
    @Autowired
    private IBizCustomerRechargeService bizCustomerRechargeService;

    /**
     * 用户钱包余额
     */
    @GetMapping("/balance")
    public AjaxResult balance() {
        BizCustomerWallet bizCustomerWallet = bizCustomerWalletService.customerWallet(SecurityUtils.getUserId());
        return AjaxResult.success(bizCustomerWallet);
    }
    /**
     * 查询用户充值套餐列表
     */
    @Log(title = "用户充值套餐列表", businessType = BusinessType.OTHER)
    @PostMapping("/recharge/item/list")
    public TableDataInfo list(BizRechargePackageItem packageItem) {
        long rpId = ToolUtils.objTOLong(configService.selectConfigByKey("recharge:item:id"));
        startPage();
        rpId = rpId==0?1L:rpId;
        packageItem.setRpId(rpId);
        SortSupport.startOrderBy("sort","asc");
        List<BizRechargePackageItem> list = rechargePackageItemService.selectBizRechargePackageItemList(packageItem);
        return getDataTable(list);
    }
    /**
     * 充值
     */
    @GetMapping("/recharge")
    public AjaxResult recharge(BigDecimal amount) {
        return AjaxResult.success("充值发起成功",wechatPayHandler.recharge(amount));
    }
    /**
     * 查询用户充值列表
     */
    @PostMapping("/recharge/list")
    public TableDataInfo list(BizCustomerRecharge bizCustomerRecharge) {
        bizCustomerRecharge.setCustomerId(SecurityUtils.getUserId());
        startPage();
        List<BizCustomerRecharge> list = bizCustomerRechargeService.selectBizCustomerRechargeList(bizCustomerRecharge);
        return getDataTable(list);
    }

    /**
     * 查询支付状态
     */
    @Log(title = "查询支付状态", businessType = BusinessType.INSERT)
    @GetMapping("/recharge/status/query/{orderNo}")
    public AjaxResult payStatusQuery(@PathVariable String orderNo) {
        log.info("payStatusQuery:::req: {}", orderNo);
        //处理订单
        Map<String, Object> res = wechatPayHandler.rechargeStatus(orderNo);
        if("200".equals(res.get("code"))){
            return AjaxResult.success("支付成功",orderNo);
        }
        return AjaxResult.error(res.get("message").toString(),orderNo);
    }

    /**
     * 查询用户资金记录列表
     */
    @PostMapping("/bill")
    public TableDataInfo bill(BizCustomerTranLog bizCustomerTranLog) {
        startPage();
        bizCustomerTranLog.setCustomerId(SecurityUtils.getUserId());
        bizCustomerTranLog.setStatus(EnableStatus.ENABLE.getCode());
        List<BizCustomerTranLog> list = bizCustomerTranLogService.selectBizCustomerTranLogList(bizCustomerTranLog);
        return getDataTable(list);
    }
}
